package com.itheima.userprofile.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.userprofile.bean.dto.TagDto;
import com.itheima.userprofile.bean.dto.TagModel;
import com.itheima.userprofile.bean.http.HttpResult;
import com.itheima.userprofile.service.TagService;
import com.itheima.userprofile.util.Codes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TagController {

    @Autowired
    private TagService tagService;

    /**
     * 添加123级标签.
     * [{"label":"一级标签名称","name":"电商","level":"1"},
     * {"label":"二级标签名称","name":"京东商城","level":"2"},
     * {"label":"三级标签名称","name":"人口属性","level":"3"}]
     * @RequestBody 将请求的Json转换为对象
     */

    @PutMapping("tags/relation") //限制put请求方式
    public void addTags(@RequestBody List<TagDto> list){
        //将list保存到数据库,将list交给service进行处理
        tagService.saveAll(list);
    }

    //查询123级标签列表
    @GetMapping("tags")
    public HttpResult<List<TagDto>> findTags(Long pid){
        List<TagDto> list = tagService.findTagsByPid(pid);
        return new HttpResult<List<TagDto>>(Codes.SUCCESS,"成功",list);
    }

    //获取4级标签创建代码
    @PutMapping("tags/model")
    public void saveTagModel(@RequestBody TagModel tagModel){
        //获取对应的标签和模型
        tagService.saveTagModel(tagModel.getTag(),tagModel.getModel());
    }

    //获取4级标签列表
    @GetMapping("tags/model")
    public HttpResult getTagModel(Long pid){
        //根据3级标签的pid获取4级标签和模型
        List<TagModel> list = null;
        list = tagService.findAllTagModel(pid);

        HttpResult<List<TagModel>> result = new HttpResult<>(Codes.SUCCESS, "查询成功", list);
        String json = JSON.toJSONString(result);
        System.out.println(json);
        return result;
    }

    //创建5级标签
    @PutMapping("tags/data")
    public HttpResult putFiveTag(@RequestBody TagDto dto){
        tagService.saveTag(dto);
        return new HttpResult(Codes.SUCCESS,"5级标签创建成功", dto);
    }
}
